package com.zllkj.mm.util

import android.content.Context
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.StaggeredGridLayoutManager


class StaggeredDividerItemDecoration : ItemDecoration {

    private var context: Context? = null
    private var interval = 0f
    private var spanCount = 0

    constructor(context: Context , interval: Float , spanCount : Int){
        this.context = context;
        this.interval = interval;
        this.spanCount = spanCount;
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val params = view.getLayoutParams() as StaggeredGridLayoutManager.LayoutParams
        // 获取item在span中的下标
        val spanIndex = params.spanIndex
        val interval = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                this.interval, context?.getResources()?.getDisplayMetrics())
        // 中间间隔
        /**
         * 这个判断适用于瀑布流只有两列的情况，如果有多列，那么再增加spanIndex % spanCount == 的判断并做处理就好了
         * 此处的left和right都为interval / 2的原因是为了让左边item和右边item同宽
         */

        if (spanIndex % spanCount == 0) {
            outRect.right = (interval / 2.0f).toInt()
        } else {
            outRect.left = (interval / 2.0f).toInt()
        }

        // 下方间隔
        outRect.bottom = interval.toInt()
    }

}